package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoEditActivity extends BaseActivity {
    @BindView(R.id.edit_txt)
    EditText editTxt;
    @BindView(R.id.btn_res)
    Button btnRes;
    @BindView(R.id.toolber_color_title)
    TextView toolberTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfoedit);
        ButterKnife.bind(this);
        info();
    }

    private void info() {
        String titleName = "标题";
        String data = getIntent().getExtras().getString("data");
        if (data.equals("nicheng")) {
            titleName = "修改昵称";
            editTxt.setHint("请输入要修改的昵称");
        } else if (data.equals("xingming")) {
            titleName = "修改真实姓名";
            editTxt.setHint("请输入要修改的姓名");
        } else if (data.equals("shenfenzheng")) {
            titleName = "修改身份证号";
            editTxt.setHint("请输入要修改的身份证号");
        } else if (data.equals("shouhuodizhi")) {
            titleName = "修改收货地址";
            editTxt.setHint("请输入要修改的收货地址");
        }

        toolberTitle.setText(titleName);
    }

    private void initBtn() {
        final Gson mGson = new Gson();
        String url = Constants.modifyusernickname;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("修改昵称", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        Intent intent = new Intent();
                        intent.putExtra("data", editTxt.getText().toString());
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userNickname", editTxt.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @OnClick({R.id.toolber_color_back, R.id.btn_res})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                Tools.showOrHide(this, this);
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.btn_res:
                if (Tools.isEmpeyNull(editTxt.getText().toString())) {
                    initBtn();
                } else {
                    toast("请输入要修改的数据");
                }
                break;
        }
    }
}
